package com.hzqx.syndata.threadpool;

import org.apache.tomcat.util.threads.ThreadPoolExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * @author yinzhiping
 * 使用ThreadPoolExcutor的原始方式创建线程池
 */
public class ThreadPoolExcutorsMainTest {
    public static void main(String[] args) {
        //自定义线程池
        ExecutorService executorService = new ThreadPoolExecutor(
                2,
                5,
                1200,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(7),
                Executors.defaultThreadFactory(),
                new java.util.concurrent.ThreadPoolExecutor.AbortPolicy()
                );
        try {
            //拒绝策略：AbortPolicy超出最大承载就会抛出异常
            for (int i = 0; i < 10; i++) {
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }
}
